<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <!-- <el-form-item label="生产单id" prop="productOrderId">
        <el-input v-model="formData.productOrderId" placeholder="请输入生产单id" />
      </el-form-item> -->
       <el-form-item label="类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MATERIAL_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="计算长" prop="calculatedLength">
        <el-input v-model="formData.calculatedLength" placeholder="请输入计算长" />
      </el-form-item>
      <el-form-item label="计算宽" prop="calculatedWidth">
        <el-input v-model="formData.calculatedWidth" placeholder="请输入计算宽" />
      </el-form-item>
      <el-form-item label="采购长" prop="purchasedLength">
        <el-input v-model="formData.purchasedLength" placeholder="请输入采购长" />
      </el-form-item>
      <el-form-item label="采购宽" prop="purchasedWidth">
        <el-input v-model="formData.purchasedWidth" placeholder="请输入采购宽" />
      </el-form-item>
      <el-form-item label="采购数量" prop="purchasedQuantity">
        <el-input v-model="formData.purchasedQuantity" placeholder="请输入采购数量" />
      </el-form-item>
      <el-form-item label="需求数" prop="demandQuantity">
        <el-input v-model="formData.demandQuantity" placeholder="请输入需求数" />
      </el-form-item>
      <el-form-item label="长开" prop="longCut">
        <el-input v-model="formData.longCut" placeholder="请输入长开" />
      </el-form-item>
      <el-form-item label="宽开" prop="wideCut">
        <el-input v-model="formData.wideCut" placeholder="请输入宽开" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-select v-model="formData.unit" placeholder="请选择单位">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.VALUATION_UNIT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="横压1" prop="horizontalPress1">
        <el-input v-model="formData.horizontalPress1" placeholder="请输入横压1" />
      </el-form-item>
      <el-form-item label="横压2" prop="horizontalPress2">
        <el-input v-model="formData.horizontalPress2" placeholder="请输入横压2" />
      </el-form-item>
      <el-form-item label="横压3" prop="horizontalPress3">
        <el-input v-model="formData.horizontalPress3" placeholder="请输入横压3" />
      </el-form-item>
      <el-form-item label="横压4" prop="horizontalPress4">
        <el-input v-model="formData.horizontalPress4" placeholder="请输入横压4" />
      </el-form-item>
      <el-form-item label="横压5" prop="horizontalPress5">
        <el-input v-model="formData.horizontalPress5" placeholder="请输入横压5" />
      </el-form-item>
      <el-form-item label="纵压1" prop="verticalPress1">
        <el-input v-model="formData.verticalPress1" placeholder="请输入纵压1" />
      </el-form-item>
      <el-form-item label="纵压2" prop="verticalPress2">
        <el-input v-model="formData.verticalPress2" placeholder="请输入纵压2" />
      </el-form-item>
      <el-form-item label="纵压3" prop="verticalPress3">
        <el-input v-model="formData.verticalPress3" placeholder="请输入纵压3" />
      </el-form-item>
      <el-form-item label="纵压4" prop="verticalPress4">
        <el-input v-model="formData.verticalPress4" placeholder="请输入纵压4" />
      </el-form-item>
      <el-form-item label="纵压5" prop="verticalPress5">
        <el-input v-model="formData.verticalPress5" placeholder="请输入纵压5" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <!-- <el-form-item label="材质编号" prop="materialCode">
        <el-input v-model="formData.materialCode" placeholder="请输入材质编号" />
      </el-form-item>
      <el-form-item label="材质名称" prop="materialName">
        <el-input v-model="formData.materialName" placeholder="请输入材质名称" />
      </el-form-item> -->
      <el-form-item label="材质ID" prop="materialId">
        <el-input v-model="formData.materialId" placeholder="请输入材质ID" />
      </el-form-item>
	  <el-form-item label="材质名称" prop="materialId">
	    <el-select v-model="formData.materialId" placeholder="请选择材质" clearable @change="handleMaterialChange">
	      <el-option
	        v-for="item in materialList"
	        :key="item.id"
	        :label="item.materialName"
	        :value="item.id"
	      />
	    </el-select>
	  </el-form-item>
	  <el-form-item label="压线类型" prop="pressLineType">
	    <el-select v-model="formData.pressLineType" placeholder="请选择压线类型" clearable @change="handlePressingTypeChange">
	      <el-option
	        v-for="item in pressingTypeList"
	        :key="item.id"
	        :label="item.pressingTypeName"
	        :value="item.id"
	      />
	    </el-select>
	  </el-form-item>
      <el-form-item label="数量比" prop="quantityRatio">
        <el-input v-model="formData.quantityRatio" placeholder="请输入数量比" />
      </el-form-item>
      <el-form-item label="已采购净板数" prop="purchasedCleanBoardQuantity">
        <el-input v-model="formData.purchasedCleanBoardQuantity" placeholder="请输入已采购净板数" />
      </el-form-item>
      <el-form-item label="用料来源" prop="materialSource">
        <el-select v-model="formData.materialSource" placeholder="请选择用料来源">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SOURCE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="计算数量" prop="calculatedQuantity">
        <el-input v-model="formData.calculatedQuantity" placeholder="请输入计算数量" />
      </el-form-item>
      <el-form-item label="印刷预损数" prop="printingLossQuantity">
        <el-input v-model="formData.printingLossQuantity" placeholder="请输入印刷预损数" />
      </el-form-item>
      <el-form-item label="供应商ID" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入供应商ID" />
      </el-form-item>
      <el-form-item label="供应商编号" prop="supplierCode">
        <el-input v-model="formData.supplierCode" placeholder="请输入供应商编号" />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input v-model="formData.supplierName" placeholder="请输入供应商名称" />
      </el-form-item>
      <el-form-item label="总件" prop="totalItems">
        <el-input v-model="formData.totalItems" placeholder="请输入总件" />
      </el-form-item>
	  <el-form-item label="毛板系数" prop="grossBoardCoefficient">
	    <el-input v-model="formData.grossBoardCoefficient" placeholder="请输入毛板系数" />
	  </el-form-item>
      <el-form-item label="净板系数" prop="cleanBoardCoefficient">
        <el-input v-model="formData.cleanBoardCoefficient" placeholder="请输入净板系数" />
      </el-form-item>
      <el-form-item label="面积报价" prop="areaQuotation">
        <el-input v-model="formData.areaQuotation" placeholder="请输入面积报价" />
      </el-form-item>
      <el-form-item label="毛板面积" prop="grossBoardArea">
        <el-input v-model="formData.grossBoardArea" placeholder="请输入毛板面积" />
      </el-form-item>
      <el-form-item label="净板面积" prop="netBoardArea">
        <el-input v-model="formData.netBoardArea" placeholder="请输入净板面积" />
      </el-form-item>
      <el-form-item label="单个核算成本" prop="singleCalculationCost">
        <el-input v-model="formData.singleCalculationCost" placeholder="请输入单个核算成本" />
      </el-form-item>
      <el-form-item label="厚度" prop="thickness">
        <el-input v-model="formData.thickness" placeholder="请输入厚度" />
      </el-form-item>
      <el-form-item label="底数" prop="bottomNumber">
        <el-input v-model="formData.bottomNumber" placeholder="请输入底数" />
      </el-form-item>
      <el-form-item label="单品用量" prop="singleItemConsumption">
        <el-input v-model="formData.singleItemConsumption" placeholder="请输入单品用量" />
      </el-form-item>
      <el-form-item label="总开数" prop="totalOpenCount">
        <el-input v-model="formData.totalOpenCount" placeholder="请输入总开数" />
      </el-form-item>
      <el-form-item label="模数" prop="modulus">
        <el-input v-model="formData.modulus" placeholder="请输入模数" />
      </el-form-item>
      <el-form-item label="密度" prop="density">
        <el-input v-model="formData.density" placeholder="请输入密度" />
      </el-form-item>
      <el-form-item label="刀模编号" prop="dieCuttingNumber">
        <el-input v-model="formData.dieCuttingNumber" placeholder="请输入刀模编号" />
      </el-form-item>
      <el-form-item label="刀模ID" prop="dieCuttingId">
        <el-input v-model="formData.dieCuttingId" placeholder="请输入刀模ID" />
      </el-form-item>
      <el-form-item label="模切数" prop="dieCuttingQuantity">
        <el-input v-model="formData.dieCuttingQuantity" placeholder="请输入模切数" />
      </el-form-item>
      <el-form-item label="模长" prop="dieCuttingLength">
        <el-input v-model="formData.dieCuttingLength" placeholder="请输入模长" />
      </el-form-item>
      <el-form-item label="模宽" prop="dieCuttingWidth">
        <el-input v-model="formData.dieCuttingWidth" placeholder="请输入模宽" />
      </el-form-item>
      <el-form-item label="需彩购数量" prop="needColorPurchaseQuantity">
        <el-input v-model="formData.needColorPurchaseQuantity" placeholder="请输入需彩购数量" />
      </el-form-item>
      <el-form-item label="部件长" prop="componentLength">
        <el-input v-model="formData.componentLength" placeholder="请输入部件长" />
      </el-form-item>
      <el-form-item label="部件宽" prop="componentWidth">
        <el-input v-model="formData.componentWidth" placeholder="请输入部件宽" />
      </el-form-item>
      <el-form-item label="部件厚度" prop="componentThickness">
        <el-input v-model="formData.componentThickness" placeholder="请输入部件厚度" />
      </el-form-item>
      <el-form-item label="切刀长度" prop="cuttingToolLength">
        <el-input v-model="formData.cuttingToolLength" placeholder="请输入切刀长度" />
      </el-form-item>
      <el-form-item label="压线长度" prop="pressLineLength">
        <el-input v-model="formData.pressLineLength" placeholder="请输入压线长度" />
      </el-form-item>
      <el-form-item label="损耗率" prop="lossRate">
        <el-input v-model="formData.lossRate" placeholder="请输入损耗率" />
      </el-form-item>
      <el-form-item label="单片重量" prop="singleWeight">
        <el-input v-model="formData.singleWeight" placeholder="请输入单片重量" />
      </el-form-item>
      <el-form-item label="单个产出数" prop="singleOutputQuantity">
        <el-input v-model="formData.singleOutputQuantity" placeholder="请输入单个产出数" />
      </el-form-item>
      <el-form-item label="单个原料重量" prop="singleRawMaterialWeight">
        <el-input v-model="formData.singleRawMaterialWeight" placeholder="请输入单个原料重量" />
      </el-form-item>
      <el-form-item label="卷数" prop="coilNumber">
        <el-input v-model="formData.coilNumber" placeholder="请输入卷数" />
      </el-form-item>
      <el-form-item label="库存数" prop="inventoryQuantity">
        <el-input v-model="formData.inventoryQuantity" placeholder="请输入库存数" />
      </el-form-item>
      <el-form-item label="总长" prop="totalLength">
        <el-input v-model="formData.totalLength" placeholder="请输入总长" />
      </el-form-item>
      <el-form-item label="长损耗" prop="lengthLoss">
        <el-input v-model="formData.lengthLoss" placeholder="请输入长损耗" />
      </el-form-item>
      <el-form-item label="宽损耗" prop="widthLoss">
        <el-input v-model="formData.widthLoss" placeholder="请输入宽损耗" />
      </el-form-item>
      <el-form-item label="每卷长度" prop="perRollLength">
        <el-input v-model="formData.perRollLength" placeholder="请输入每卷长度" />
      </el-form-item>
      <el-form-item label="比例数" prop="ratioQuantity">
        <el-input v-model="formData.ratioQuantity" placeholder="请输入比例数" />
      </el-form-item>
      <el-form-item label="可用库存" prop="availableInventory">
        <el-input v-model="formData.availableInventory" placeholder="请输入可用库存" />
      </el-form-item>
      <el-form-item label="客户预损" prop="customerLoss">
        <el-input v-model="formData.customerLoss" placeholder="请输入客户预损" />
      </el-form-item>
      <el-form-item label="参数1" prop="parameter1">
        <el-input v-model="formData.parameter1" placeholder="请输入参数1" />
      </el-form-item>
      <el-form-item label="参数2" prop="parameter2">
        <el-input v-model="formData.parameter2" placeholder="请输入参数2" />
      </el-form-item>
      <el-form-item label="参数3" prop="parameter3">
        <el-input v-model="formData.parameter3" placeholder="请输入参数3" />
      </el-form-item>
      <el-form-item label="参数4" prop="parameter4">
        <el-input v-model="formData.parameter4" placeholder="请输入参数4" />
      </el-form-item>
      <el-form-item label="参数5" prop="parameter5">
        <el-input v-model="formData.parameter5" placeholder="请输入参数5" />
      </el-form-item>
      <el-form-item label="参数6" prop="parameter6">
        <el-input v-model="formData.parameter6" placeholder="请输入参数6" />
      </el-form-item>
      <el-form-item label="参数7" prop="parameter7">
        <el-input v-model="formData.parameter7" placeholder="请输入参数7" />
      </el-form-item>
      <el-form-item label="参数8" prop="parameter8">
        <el-input v-model="formData.parameter8" placeholder="请输入参数8" />
      </el-form-item>
      <el-form-item label="参数9" prop="parameter9">
        <el-input v-model="formData.parameter9" placeholder="请输入参数9" />
      </el-form-item>
      <el-form-item label="参数10" prop="parameter10">
        <el-input v-model="formData.parameter10" placeholder="请输入参数10" />
      </el-form-item>
      <el-form-item label="报价单位" prop="quotationUnit">
        <el-select v-model="formData.quotationUnit" placeholder="请选择报价单位">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.VALUATION_UNIT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="成本计算公式" prop="costCalculationFormula">
        <el-input v-model="formData.costCalculationFormula" placeholder="请输入成本计算公式" />
      </el-form-item>
      <el-form-item label="用料平方" prop="materialSquareMeter">
        <el-input v-model="formData.materialSquareMeter" placeholder="请输入用料平方" />
      </el-form-item>
      <el-form-item label="修边" prop="trimming">
        <el-input v-model="formData.trimming" placeholder="请输入修边" />
      </el-form-item>
      <el-form-item label="后工序预损" prop="postProcessLoss">
        <el-input v-model="formData.postProcessLoss" placeholder="请输入后工序预损" />
      </el-form-item>
      <el-form-item label="备注1" prop="remark1">
        <el-input v-model="formData.remark1" placeholder="请输入备注1" />
      </el-form-item>
      <el-form-item label="备注2" prop="remark2">
        <el-input v-model="formData.remark2" placeholder="请输入备注2" />
      </el-form-item>
      <el-form-item label="备注3" prop="remark3">
        <el-input v-model="formData.remark3" placeholder="请输入备注3" />
      </el-form-item>
      <el-form-item label="备注4" prop="remark4">
        <el-input v-model="formData.remark4" placeholder="请输入备注4" />
      </el-form-item>
      <el-form-item label="备注5" prop="remark5">
        <el-input v-model="formData.remark5" placeholder="请输入备注5" />
      </el-form-item>
      <el-form-item label="占用标签" prop="labelUsage">
        <el-input v-model="formData.labelUsage" placeholder="请输入占用标签" />
      </el-form-item>
      <el-form-item label="库存占用数" prop="inventoryOccupyQuantity">
        <el-input v-model="formData.inventoryOccupyQuantity" placeholder="请输入库存占用数" />
      </el-form-item>
      <el-form-item label="原纸规格" prop="rawMaterialSpecification">
        <el-input v-model="formData.rawMaterialSpecification" placeholder="请输入原纸规格" />
      </el-form-item>
      <el-form-item label="规格计算类型" prop="specificationCalculationType">
        <el-select v-model="formData.specificationCalculationType" placeholder="请选择规格计算类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { SaleBomApi } from '@/api/erp/salebom'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  productOrderId: undefined,
  bomId: undefined,
  type: undefined,
  calculatedLength: undefined,
  calculatedWidth: undefined,
  purchasedLength: undefined,
  purchasedWidth: undefined,
  purchasedQuantity: undefined,
  demandQuantity: undefined,
  longCut: undefined,
  wideCut: undefined,
  unit: undefined,
  horizontalPress1: undefined,
  horizontalPress2: undefined,
  horizontalPress3: undefined,
  horizontalPress4: undefined,
  horizontalPress5: undefined,
  verticalPress1: undefined,
  verticalPress2: undefined,
  verticalPress3: undefined,
  verticalPress4: undefined,
  verticalPress5: undefined,
  remark: undefined,
  materialCode: undefined,
  materialName: undefined,
  materialId: undefined,
  pressLineType: undefined,
  quantityRatio: undefined,
  purchasedCleanBoardQuantity: undefined,
  materialSource: undefined,
  calculatedQuantity: undefined,
  printingLossQuantity: undefined,
  supplierId: undefined,
  supplierCode: undefined,
  supplierName: undefined,
  totalItems: undefined,
  grossBoardCoefficient: undefined,
  cleanBoardCoefficient: undefined,
  areaQuotation: undefined,
  grossBoardArea: undefined,
  netBoardArea: undefined,
  singleCalculationCost: undefined,
  thickness: undefined,
  bottomNumber: undefined,
  singleItemConsumption: undefined,
  totalOpenCount: undefined,
  modulus: undefined,
  density: undefined,
  dieCuttingNumber: undefined,
  dieCuttingId: undefined,
  dieCuttingQuantity: undefined,
  dieCuttingLength: undefined,
  dieCuttingWidth: undefined,
  needColorPurchaseQuantity: undefined,
  componentLength: undefined,
  componentWidth: undefined,
  componentThickness: undefined,
  cuttingToolLength: undefined,
  pressLineLength: undefined,
  lossRate: undefined,
  singleWeight: undefined,
  singleOutputQuantity: undefined,
  singleRawMaterialWeight: undefined,
  coilNumber: undefined,
  inventoryQuantity: undefined,
  totalLength: undefined,
  lengthLoss: undefined,
  widthLoss: undefined,
  perRollLength: undefined,
  ratioQuantity: undefined,
  availableInventory: undefined,
  customerLoss: undefined,
  parameter1: undefined,
  parameter2: undefined,
  parameter3: undefined,
  parameter4: undefined,
  parameter5: undefined,
  parameter6: undefined,
  parameter7: undefined,
  parameter8: undefined,
  parameter9: undefined,
  parameter10: undefined,
  quotationUnit: undefined,
  costCalculationFormula: undefined,
  materialSquareMeter: undefined,
  trimming: undefined,
  postProcessLoss: undefined,
  remark1: undefined,
  remark2: undefined,
  remark3: undefined,
  remark4: undefined,
  remark5: undefined,
  labelUsage: undefined,
  inventoryOccupyQuantity: undefined,
  rawMaterialSpecification: undefined,
  specificationCalculationType: undefined
})
const formRules = reactive({
 
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, bomId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.bomId = bomId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await SaleBomApi.getSaleBomMaterial(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await SaleBomApi.createSaleBomMaterial(data)
      message.success(t('common.createSuccess'))
    } else {
      await SaleBomApi.updateSaleBomMaterial(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    productOrderId: undefined,
    bomId: undefined,
    type: undefined,
    calculatedLength: undefined,
    calculatedWidth: undefined,
    purchasedLength: undefined,
    purchasedWidth: undefined,
    purchasedQuantity: undefined,
    demandQuantity: undefined,
    longCut: undefined,
    wideCut: undefined,
    unit: undefined,
    horizontalPress1: undefined,
    horizontalPress2: undefined,
    horizontalPress3: undefined,
    horizontalPress4: undefined,
    horizontalPress5: undefined,
    verticalPress1: undefined,
    verticalPress2: undefined,
    verticalPress3: undefined,
    verticalPress4: undefined,
    verticalPress5: undefined,
    remark: undefined,
    materialCode: undefined,
    materialName: undefined,
    materialId: undefined,
    pressLineType: undefined,
    quantityRatio: undefined,
    purchasedCleanBoardQuantity: undefined,
    materialSource: undefined,
    calculatedQuantity: undefined,
    printingLossQuantity: undefined,
    supplierId: undefined,
    supplierCode: undefined,
    supplierName: undefined,
    totalItems: undefined,
	grossBoardCoefficient: undefined,
    cleanBoardCoefficient: undefined,
    areaQuotation: undefined,
    grossBoardArea: undefined,
    netBoardArea: undefined,
    singleCalculationCost: undefined,
    thickness: undefined,
    bottomNumber: undefined,
    singleItemConsumption: undefined,
    totalOpenCount: undefined,
    modulus: undefined,
    density: undefined,
    dieCuttingNumber: undefined,
    dieCuttingId: undefined,
    dieCuttingQuantity: undefined,
    dieCuttingLength: undefined,
    dieCuttingWidth: undefined,
    needColorPurchaseQuantity: undefined,
    componentLength: undefined,
    componentWidth: undefined,
    componentThickness: undefined,
    cuttingToolLength: undefined,
    pressLineLength: undefined,
    lossRate: undefined,
    singleWeight: undefined,
    singleOutputQuantity: undefined,
    singleRawMaterialWeight: undefined,
    coilNumber: undefined,
    inventoryQuantity: undefined,
    totalLength: undefined,
    lengthLoss: undefined,
    widthLoss: undefined,
    perRollLength: undefined,
    ratioQuantity: undefined,
    availableInventory: undefined,
    customerLoss: undefined,
    parameter1: undefined,
    parameter2: undefined,
    parameter3: undefined,
    parameter4: undefined,
    parameter5: undefined,
    parameter6: undefined,
    parameter7: undefined,
    parameter8: undefined,
    parameter9: undefined,
    parameter10: undefined,
    quotationUnit: undefined,
    costCalculationFormula: undefined,
    materialSquareMeter: undefined,
    trimming: undefined,
    postProcessLoss: undefined,
    remark1: undefined,
    remark2: undefined,
    remark3: undefined,
    remark4: undefined,
    remark5: undefined,
    labelUsage: undefined,
    inventoryOccupyQuantity: undefined,
    rawMaterialSpecification: undefined,
    specificationCalculationType: undefined
  }
  formRef.value?.resetFields()
}

// ** 工序列表 ** 
const pressingTypeList = ref<any[]>([])  // 适应任何返回的数据结构
// 材质列表
const materialList = ref<{ id: string; materialName: string }[]>([])

/** 获取工序列表 */
const getAllPressingType = async () => {
  try {
    const response = await SaleBomApi.getAllBasePressingType();
    if (response) {
      pressingTypeList.value = response;
    }
  } catch (error) {
    console.error("获取压线类型列表失败:", error);
  }
}

/** 获取材质列表 */
const getAllBaseMaterial = async () => {
  try {
    const response = await SaleBomApi.getAllBaseMaterial();
    if (response) {
      materialList.value = response;
    }
  } catch (error) {
    console.error("获取材质列表失败:", error);
  }
}; 

/** 选择工序时，自动填充工序内容 */
const handlePressingTypeChange = (id: string) => {
  const selectedPressingType = pressingTypeList.value.find(item => item.id === id);
  formData.value.pressLineType = selectedPressingType ? selectedPressingType.pressingTypeName : "";
}

/** 选择材质时，自动填充材质名称 */
const handleMaterialChange = (id: string) => {
  const selectedMaterial = materialList.value.find(item => item.id === id);
  formData.value.materialName = selectedMaterial ? selectedMaterial.materialName : "";
};

onMounted(() => {
  Promise.all([
	getAllPressingType(),
	getAllBaseMaterial() 
  ])
})
</script>